package com.example.demo.datasource;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.example.demo.dao.SoDivisionsaleMapper;
import com.example.demo.model.SoDivisionsale;

@Component
@Configuration
@EnableScheduling
public class WriteDataTxt {
    private final Logger logger = LoggerFactory.getLogger(WriteDataTxt.class);

    @Value("${filePath}")
    private String filePath;

    @Autowired
    private SoDivisionsaleMapper soDivisionsaleMapper;

    // private final String E1filePath = filePath+"E1 Data"+"/";
   // @Scheduled(cron = "0 */1 * * * ?")
	@Scheduled(cron = "0 0 9 * * ?")
    public void start() {
        logger.info("->启动定时任务");
        GetSODivisionSaleRepData();
        GetDivision_rep_backlog();
    }

    public void GetSODivisionSaleRepData() {
        logger.info("->开始写vw_division_rep_revenue数据到Txt文件中！");
        List<SoDivisionsale> list = soDivisionsaleMapper.selectByDivison();
        if (list != null && list.size() > 0) {
            File file = new File(filePath + "vw_division_rep_revenue.txt");
            try {
                if (!file.exists()) {
                    File.createTempFile(filePath, null);
                }
                BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                writer.write("Order Number" + "	" + "China Division" + "	" + "Sales Rep Local Name" + "\r\n");
                for (SoDivisionsale so : list) {
                    writer.write(so.getOrderNumber() + "	" + so.getChinaDivision() + "	" + so.getSalesReplocalName() + "\r\n");
                }
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            logger.info("->The vw_division_rep_revenue table data is empty！");
        }

    }

    public void GetDivision_rep_backlog() {
        logger.info("->开始写vw_division_rep_backlog数据到Txt文件中！");
        List<SoDivisionsale> backlogList = soDivisionsaleMapper.selectByExample(null);
        if (backlogList != null && backlogList.size() > 0) {
            File file = new File(filePath + "vw_division_rep_backlog.txt");
            try {
                if (!file.exists()) {
                    File.createTempFile(filePath, null);
                }
                BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                writer.write("Order Number" + "	" + "China Division" + "	" + "Sales Rep Local Name" + "\r\n");
                for (SoDivisionsale so : backlogList) {
                    writer.write(so.getOrderNumber() + "	" + so.getChinaDivision() + "	" + so.getSalesReplocalName() + "\r\n");
                }
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            logger.info("->The vw_division_rep_backlog table data is empty！");
        }

    }


}
